package com.talent.jiwen.training;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.http.result.tiku.SectionBean;
import com.talent.jiwen.training.data.IdNameBean;

import java.util.HashSet;
import java.util.List;

/**
 * Created by Jason on 2018/5/22.
 */

public class SelectChapterAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<SectionBean.ChildBeanX> dataList;

    public SelectChapterAdapter(Context context, List<SectionBean.ChildBeanX> dataList , IdNameBean selectItem) {
        this.context = context;
        if (null != selectItem){
            String idStr = selectItem.getIdStr();
            String[] idArrays = idStr.split(",");

            HashSet<Integer> hashSet = new HashSet<>();


            for(int i = 0 ; i < idArrays.length ; i ++){
                hashSet.add(Integer.valueOf(idArrays[i]));
            }

            for(SectionBean.ChildBeanX bigData : dataList){
                if (hashSet.contains(bigData.getZsd_id())){
                    bigData.setChecked(true);
                    for(SectionBean.ChildBeanX.ChildBean smallData : bigData.getChild()){
                        smallData.setChecked(true);
                    }
                }else{
                    bigData.setChecked(false);
                    for(SectionBean.ChildBeanX.ChildBean small : bigData.getChild()){
                        if(hashSet.contains(small.getZsd_id())){
                            small.setChecked(true);
                        }else{
                            small.setChecked(false);
                        }
                    }
                }
            }
        }
        this.dataList = dataList;
    }

    @Override
    public int getGroupCount() {
        return dataList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return dataList.get(i).getChild().size();
    }

    @Override
    public Object getGroup(int i) {
        return dataList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return dataList.get(i).getChild().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (null == convertView){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chapter_expand_group , parent , false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.checkIv = (ImageView)convertView.findViewById(R.id.checkIv);
            groupViewHolder.titleTv = (TextView)convertView.findViewById(R.id.titleTv);
            groupViewHolder.arrowIv = (ImageView)convertView.findViewById(R.id.arrowIv);
            convertView.setTag(groupViewHolder);
        }else{
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }

        final SectionBean.ChildBeanX data = dataList.get(groupPosition);
        groupViewHolder.titleTv.setText(data.getZsd_name());
        if (data.isChecked()){
            groupViewHolder.checkIv.setImageResource(R.mipmap.iv_theme_check_selected);
        }else{
            groupViewHolder.checkIv.setImageResource(R.mipmap.iv_theme_check_normal);
        }

        groupViewHolder.checkIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean childCheck = false;
                if (data.isChecked()){
                    data.setChecked(false);
                    childCheck = false;
                }else{
                    data.setChecked(true);
                    childCheck = true;
                }

                for(SectionBean.ChildBeanX.ChildBean childBean : data.getChild()){
                    childBean.setChecked(childCheck);
                }

                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (null == convertView){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_chapter_expand_child , parent , false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.childCheckIv  = (ImageView)convertView.findViewById(R.id.childCheckIv);
            childViewHolder.contentTv = (TextView)convertView.findViewById(R.id.contentTv);
            convertView.setTag(childViewHolder);
        }else{
            childViewHolder  = (ChildViewHolder) convertView.getTag();
        }

        final SectionBean.ChildBeanX.ChildBean data = dataList.get(groupPosition).getChild().get(childPosition);

        childViewHolder.contentTv.setText(data.getZsd_name());
        if (data.isChecked()){
            childViewHolder.childCheckIv.setImageResource(R.mipmap.iv_theme_check_selected);
        }else{
            childViewHolder.childCheckIv.setImageResource(R.mipmap.iv_theme_check_normal);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (data.isChecked()){
                    data.setChecked(false);
                }else{
                    data.setChecked(true);
                }
                int count = 0;
                for(SectionBean.ChildBeanX.ChildBean childBean : dataList.get(groupPosition).getChild()){
                    if(childBean.isChecked()){
                        count++;
                    }else{
                        break;
                    }
                }
                if (count == dataList.get(groupPosition).getChild().size()){
                    dataList.get(groupPosition).setChecked(true);
                }else{
                    dataList.get(groupPosition).setChecked(false);
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }


    /**获取选中的章节
     *
     *  选大章时不传小节
     * @return
     */
    public IdNameBean getChapterSelectItem (){
        StringBuilder ids =  new StringBuilder();
        StringBuilder names = new StringBuilder();
        for (SectionBean.ChildBeanX big : dataList){
            if (big.isChecked()){
                ids.append(big.getZsd_id()+"");
                ids.append(",");
                names.append(big.getZsd_name());
                names.append(",");
            }else{
                for (SectionBean.ChildBeanX.ChildBean small : big.getChild()){
                    if (small.isChecked()){
                        ids.append(small.getZsd_id() + "");
                        ids.append(",");
                        names.append(small.getZsd_name());
                        names.append(",");
                    }
                }
            }
        }

        if (ids.length() > 0){
            ids.deleteCharAt(ids.length() - 1);
        }
        if (names.length() > 0){
            names.deleteCharAt(names.length()  -1);
        }

        IdNameBean idNameBean = new IdNameBean();
        idNameBean.setIdStr(ids.toString());
        idNameBean.setName(names.toString());

        return idNameBean;
    }


    static class GroupViewHolder{
        ImageView checkIv;
        TextView titleTv;
        ImageView arrowIv;
    }

    static class ChildViewHolder{
        ImageView childCheckIv;
        TextView contentTv;
    }
}
